package com.jiangjiang.eshop.pay.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.jiangjiang.eshop.order.dto.OrderInfoDTO;
import com.jiangjiang.eshop.pay.Builder.PayTransactionBuilder;
import com.jiangjiang.eshop.pay.api.PayApi;
import com.jiangjiang.eshop.pay.constant.PayTransactionStatus;
import com.jiangjiang.eshop.pay.dto.PayTransactionDTO;
import com.jiangjiang.eshop.pay.dto.PayTransactionQueryCondition;
import com.jiangjiang.eshop.pay.service.PayService;
import com.jiangjiang.eshop.pay.service.PayTransactionService;
import com.jiangjiang.eshop.wms.dto.ReturnGoodsInputOrderDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassName PayServiceImpl
 * @Description TODO
 * @Author jhw
 * @Date 2021/9/9 19:49
 * @Version 1.0
 **/
@Slf4j
@Component
public class PayServiceImpl implements PayService {
    @Autowired
    private PayApi payApi;
    @Autowired
    private PayTransactionService payTransactionService;


    @Override
    public String getQrCode(OrderInfoDTO order) {
      try {
          String qrCode = payApi.getQrCode(order.getPayType(), order.getOrderNo(), order.getTotalAmount());
          //记录交易流水
          PayTransactionDTO payTransactionDTO = PayTransactionBuilder.get()
                  .setOrderRelatedData(order)
                  .initStatue()
                  .create();
          payTransactionService.save(payTransactionDTO);

          return qrCode;
      }catch (Exception e){
          log.warn("PayServiceImpl getQrCode error",e);
      }
        return null;
    }

    @Override
    public Boolean refund(ReturnGoodsInputOrderDTO returnGoodsInputOrder) {
        try {
            PayTransactionQueryCondition condition = PayTransactionQueryCondition.builder()
                    .orderNo(returnGoodsInputOrder.getOrderNo())
                    .build();
            List<PayTransactionDTO> transactionDTOList = payTransactionService.queryByCondition(condition);
            if (CollectionUtils.isEmpty(transactionDTOList)) {
                throw new RuntimeException(String.format("未查询到订单[%s]的交易流水", returnGoodsInputOrder.getOrderNo()));
            }

            PayTransactionDTO payTransaction = transactionDTOList.get(0);
            payTransaction.setStatus(PayTransactionStatus.REFUND);
            payTransactionService.update(payTransaction);
            // 执行退款操作
            return payApi.refund(
                    payTransaction.getTransactionChannel(),
                    payTransaction.getOrderNo(),
                    payTransaction.getPayableAmount()
            );

        } catch (Exception e) {
            log.warn("PayServiceImpl refund error", e);
        }
        return false;
    }

    @Override
    public void payForSupplier(String bankName, String bankAccount, String bankAccountHolder, Double totalSettlementAmount) {
        // 对接银行系统付款系统
    }
}
